package client.whiteboard.model;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.ListIterator;
public class Drawing implements IDrawing
{
	private ArrayList _drawings;
	private LinkedList<Line> _currentLines;
	private LinkedList<Point> _currentPoints;
	private LinkedList _points;
	private int _maxlen;
	private int _count;
	private int _penSize;
	public Drawing()
	{
		_drawings = new ArrayList();
		_maxlen = 1;
		_count = 0;
		_penSize = 5;
	}
	public void startDrawing()
	{
		_currentLines = new LinkedList();
		_currentPoints = new LinkedList();
	}
	public void setPenSize(int penSize)
	{
		_penSize = penSize;
	}
	public void stopDrawing()
	{
		if(_currentLines.size() > 0){
			_drawings.add(_currentLines);
		}
	}
	public void addPoint(Point p)
	{
		int size = _currentPoints.size();
		if((size+1) % 2 == 0 && size > 0){
			_currentLines.add(new Line(_currentPoints.getLast(), p, _penSize));
			_count++;
			if(_count == _maxlen){
				stopDrawing();
				startDrawing();
				_count = 0;
			}
		}
		_currentPoints.add(p);
	}
	public ListIterator getDrawingIterator()
	{
		return _drawings.listIterator();
	}
	public DrawingData getDrawingData()
	{
		return (new DrawingData(getDrawingIterator()));
	}
	public void loadDrawingData(DrawingData drawingData)
	{
		startDrawing();
		ListIterator<Line> li = drawingData._lines.listIterator();
		while(li.hasNext())
		{
			_currentLines.add((Line)li.next());
		}
		stopDrawing();
	}
	public void clear()
	{
		_drawings.clear();
	}
}
